﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using System.IO;
using OhioState.Controls;
using System.Runtime.Remoting;

namespace VolumeDataServer
{
    public partial class VolumeDataForm : Form
    {
        public VolumeDataForm()
        {
            InitializeComponent();
            AssetManager.Instance.AssetAdded += new IAssetManagedHandler(Instance_AddItemEvent);
            RemotingConfiguration.Configure("./VolumeDataServer.exe.config", false);
            RemotingServices.Marshal(AssetManager.Instance, "DataServerAssetManager");
        }

        void Instance_AddItemEvent(IAssetManaged obj)
        {
            //
            // Add the item to the listView.
            //
            volumeListBox.Items.Add(obj);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void volumeFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (volumeOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                String filename = this.volumeOpenFileDialog.FileName;
                String extension = System.IO.Path.GetExtension(filename);
                String name = Path.GetFileName(filename);
                AssetManager assetManager = AssetManager.Instance;

                if (extension == ".vol" || extension == ".dat")
                {
                    Volume vol = new Volume(filename);
                    if (assetManager.Find(typeof(Volume), name) == null)
                        assetManager.Add((Volume)vol);
                }

                else if (extension == ".raw")
                {
                    VolumeDim volDim = new VolumeDim(filename);
                    if (volDim.ShowDialog() == DialogResult.OK)
                    {
                        TexelFormat type = new TexelFormat();
                        type.Type = (ByteFormat)volDim.TexelType.Type;
                        type.Format = OhioState.Graphics.ImageFormat.ALPHA;
                        Volume vol = (Volume)new Volume(filename, volDim.XDim, volDim.YDim, volDim.ZDim, type);
                        if (assetManager.Find(typeof(Volume), name) == null)
                            assetManager.Add((Volume)vol);

                    }
                }

            }
        }
    }
}
